package org.nf.box.web.controller;

import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.nf.box.common.result.ResultVO;
import org.nf.box.domain.Comment;
import org.nf.box.service.CommentService;
import org.nf.box.web.vo.CommentVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lzp
 * @Date 2024-12-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/comment")
public class CommentController {

    private final CommentService commentService;

    /**
     * 添加评论
     * @return vo
     */
    @PostMapping("/add")
    public ResultVO addComment(@Param("postId")Integer postId,
                               @Param("userId")Integer userId,
                               @Param("content")String content) {
        Comment comment = new Comment();
        comment.setPostId(postId);
        comment.setUserId(userId);
        comment.setContent(content);
        commentService.addComment(comment);
        return new ResultVO<>(200,"评论成功",comment);
    }

    /**
     * 获取帖子所有父评论
     * @param postId 帖子id
     * @return 父评论列表
     */
    @GetMapping("/gets")
    public ResultVO<List<CommentVO>> getComments(@RequestParam("postId") int postId) {
        List<CommentVO> comments = commentService.getComments(postId);
        return new ResultVO<>(200,"",comments);
    }

    /**
     * 获取指定的父评论
     * @param postI
     * @param commentId
     * @return
     */
    @GetMapping("/get")
    public ResultVO<CommentVO> getComment(@RequestParam("postId") int postI,
                                          @RequestParam("commentId") int commentId) {
        CommentVO comment = commentService.getParentCommentById(postI, commentId);
        return new ResultVO<>(200,"",comment);
    }

    /**
     * 获取总评论数
     * @param postId
     * @return
     */
    @GetMapping("total")
    public ResultVO<Integer> getTotal(@RequestParam("postId") Integer postId) {
        Integer totalComment = commentService.getTotalComment(postId);
        return new ResultVO<>(200,"",totalComment);
    }


}
